package classconverstation.cscl.ist.psu.edu.client.classdisplay;

import java.util.ArrayList;

import classconverstation.cscl.ist.psu.edu.client.messengers.Course;
import classconverstation.cscl.ist.psu.edu.client.services.LoginService;
import classconverstation.cscl.ist.psu.edu.client.services.LoginServiceAsync;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class CourseListPopup extends PopupPanel {
	
	FlowPanel fPanel;
	FlowPanel headingPanel;
	FlowPanel courseListPanel;
	ArrayList<Course> allCourses;
	Course selectedCourse;
	
	private LoginServiceAsync loginServiceSvc = GWT.create(LoginService.class);
	
	public CourseListPopup(){
		super();
		this.setWidth("300px");
		
		fPanel = new FlowPanel();
		this.add(fPanel);
		
		headingPanel = getHeadingPanel();
		
		
		fPanel.add(headingPanel);
		loadAllCourses();
		
		
	}
	
	FlowPanel getHeadingPanel(){
		
		FlowPanel headPanel = new FlowPanel();
		Label courses = new Label("Courses");
		headPanel.add(courses);
		return headPanel;
		
	}
	
	FlowPanel getCourseListPanel(){
		FlowPanel listPanel = new FlowPanel();
		ScrollPanel sPanel = new ScrollPanel();
		FlexTable listTable = new FlexTable();
		addCourses(listTable);
		sPanel.add(listTable);
		listPanel.add(sPanel);
		if (allCourses.size()>10){
			this.setHeight("300px");
		}
		
		return listPanel;
	}
	
	public void addCourses(FlexTable fTable){
		for (int i =0; i< allCourses.size(); i++ ){
			final Course c = allCourses.get(i);
			Label courseLabel = new Label(c.getCourseName());
			Button goButton = new Button("Go to Class");
			fTable.setWidget(i, 0, courseLabel );
			fTable.setWidget(i, 1, goButton );
			fTable.getCellFormatter().addStyleName(i, 0, "courseName");
			fTable.getCellFormatter().addStyleName(i, 1, "goToCell");
			
			goButton.addClickHandler(new ClickHandler(){

				public void onClick(ClickEvent event) {
					setSelectedCourse(c);
					hide();
				}
				
			});
		}
		
	}
	
	private void loadAllCourses(){
		if (loginServiceSvc == null) {
			loginServiceSvc = GWT.create(LoginService.class);
		}

		AsyncCallback<ArrayList<Course>> callback = new AsyncCallback<ArrayList<Course>>() {
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				Log.debug("Error when getting all the courses");
			}
			
			public void onSuccess(ArrayList<Course> allCoursesFromDB) {
				allCourses = allCoursesFromDB;
				//Window.alert("course loaded" + allCourses.size());
				courseListPanel = getCourseListPanel();
				fPanel.add(courseListPanel);
				
			}
		};
		loginServiceSvc.getAllCourses(callback);
	}
	
	public void  setSelectedCourse(Course c){
		this.selectedCourse = c;
	}
	public Course getSelectedCourse(){
		return this.selectedCourse;
	}
	

}
